<head>
	<link rel="stylesheet" href="css/data.css" type="text/css" />
	<link rel="stylesheet" href="css/global.css" type="text/css" />
	<link rel="stylesheet" href="css/dartomat.css" type="text/css" />
	<script type="text/javascript" src="scripts/jquery-2.0.3.min.js"></script>
	<script type="text/javascript" src="scripts/dartomat.js"></script>	
</head>
<meta name="viewport" content="width=480, user-scalable=yes">
<div id="main">
	<div class="leftPanel">
		<?php
			include "vars.php";
			include "data_include.php";
			
			print("<h1>MMS-Dart-Liga</h1>");
			
			print("<a href='statistics.php'>Statistik</a><br/><br/>");
			
			// save game
			game_save($mysqli);
			
			if (isset($_GET['savegame']) == true) {
				$res = $mysqli->query("select max(id) max_game_id from game_data");
				$row = $res->fetch_assoc();
				$id = $row['max_game_id'] + 1;
				foreach($_GET as $key => $value) {
					if (startsWith($key,'player_') && $value != "") {
						$player_id = explode("_", $key)[1];
						$sql = "INSERT INTO game_data (id,date, game_id, player_id, score) VALUES (".$id.",CURDATE(), ".$_GET['game_id'].",".$player_id.",".$value.")";
						mysqli_query($mysqli,$sql);
						$mysqli->commit();
					}
				}
			}
			
			if (isset($_GET['kw']) == true) 
			{
				$yearkw = $_GET['kw'];
				games_kw_summary($mysqli,$yearkw,$yearkw);
				games_table($mysqli, $yearkw);
			} else {
				$yearkw = date('Y').date('W');
				games_kw_summary($mysqli,$yearkw,"");
				games_table($mysqli, $yearkw);
			}
				
			
			if (isset($_GET['debug']) == true) 
			foreach($_GET as $key => $value) {
				echo "\$_GET['$key'] = $value<br/>";
			}
			
			function startsWith($haystack, $needle)
			{
				return $needle === "" || strpos($haystack, $needle) === 0;
			}
			function endsWith($haystack, $needle)
			{
				return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
			}
		?>
	</div>
	
	<div class="rightPanel">
		<?php
			include "dartboard.php";
		?>
	</div>
	
</div>